import time

from selenium.webdriver.common.by import By

from common.base import Base
from common.function import open_browser


class SearchOrder(Base):
    def bk_username(self, username):#输入用户名
        self.send_keys((By.NAME, 'username'), username)

    def bk_password(self, password):#输入密码
        self.send_keys((By.NAME, 'password'), password)

    def login_bnt(self):#点击登录按钮
        self.click((By.CLASS_NAME, 'btn-a'))

    def click_order(self):#点击订单管理
        self.swtich_iframe((By.ID, 'menu-frame'))
        self.click((By.XPATH, '//*[@id="menu-ul"]/li[4]'))
        self.switch_to_default_content()


    def search_order_num(self):#搜索框
        self.swtich_iframe((By.ID,'main-frame'))#切换到frame下
        order_num=self.elelocate((By.ID,'order_sn'))
        order_num.click()
        order_num.send_keys('2022091932103')#搜索订单号
        time.sleep(2)

    def search_order_hum(self):
        order_hum=self.elelocate((By.ID,'consignee'))#搜索收货人
        order_hum.click()
        order_hum.send_keys('dyh')
        time.sleep(2)

    def search_order_status(self):
        self.select_option((By.ID,'status'),0) #下拉菜单选择
        self.select_option((By.ID,'status'),1)
        # self.select_option((By.ID,'status'),2)
        # self.select_option((By.ID,'status'),3)
        # self.select_option((By.ID,'status'),4)

    def search_button(self):
        self.click((By.CLASS_NAME,'button'))#点击搜索按钮

    def high_search(self):
        self.click((By.CLASS_NAME,'cl-link'))#点击高级搜索

    def high_search_input(self):
        order_num1=self.elelocate((By.XPATH,"//input[@id='order_sn'and@size='30']"))#订单号输入框
        order_num1.click()
        order_num1.send_keys('2022091932103')
        order_hum = self.elelocate((By.XPATH, "//input[@id='consignee'and@size='20']"))#收货人搜索
        order_hum.click()
        order_hum.send_keys('dyh')
        ordst1=self.select_option((By.ID,'select4'),4)#选择配送方式下拉框
        ordst2 = self.select_option((By.ID, 'select5'), 2)#选择付款方式下拉框
        self.click((By.XPATH,'//button[@id="start_time_btn"]//img'))#点击开始下单时间
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[5]/table/tbody/tr[3]/td[7]'))#选择开始时间17号
        self.click((By.XPATH,'//button[@id="end_time_btn"]//img'))#点击结束时间
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/table/tbody/tr[4]/td[5]'))#选择结束时间22号
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/table/thead/tr[1]/td[3]/div'))
        ordst3 = self.select_option((By.ID, 'select9'), 2)#选择订单状态的下拉框
        time.sleep(2)
        ordst4 = self.select_option((By.ID, 'select11'), 1)#选择付款状态下拉框
        time.sleep(2)
        ordst5 = self.select_option((By.ID, 'select10'), 1)#选择收货状态下拉框
        time.sleep(2)
        self.click((By.ID,'query'))#点击搜索按钮
        time.sleep(3)

    def order_alter_pay(self):
        self.click((By.XPATH, '//*[@id="listDiv"]/table[1]/tbody/tr[3]/td[7]/a'))#点击查看按钮
        self.scroll_low()
        self.click((By.XPATH, "//input[@name='pay']"))
        self.click((By.XPATH, "//textarea[@name='action_note']"))
        self.send_keys((By.XPATH, "//textarea[@name='action_note']"), '已付款')
        self.click((By.XPATH, '//input[1]'))
        self.switch_to_default_content()

    def clickorderlist(self):  # 点击订单列表
        time.sleep(3)
        self.swtich_iframe((By.ID, 'header-frame'))
        self.click((By.XPATH, '//*[@id="menu-div"]/ul/li[4]/a'))

    def high_search1(self):
        self.switch_to_default_content()
        self.swtich_iframe((By.ID, 'main-frame'))
        self.click((By.CLASS_NAME,'cl-link'))

    def high_search_input1(self):
        order_num1=self.elelocate((By.XPATH,"//input[@id='order_sn'and@size='30']"))#订单号输入框
        order_num1.click()
        order_num1.send_keys('2022091932103')
        order_hum = self.elelocate((By.XPATH, "//input[@id='consignee'and@size='20']"))#收货人搜索
        order_hum.click()
        order_hum.send_keys('dyh')
        ordst1=self.select_option((By.ID,'select4'),4)#选择配送方式下拉框
        ordst2 = self.select_option((By.ID, 'select5'), 2)#选择付款方式下拉框
        self.click((By.XPATH,'//button[@id="start_time_btn"]//img'))#点击开始下单时间
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[5]/table/tbody/tr[3]/td[7]'))#选择开始时间17号
        self.click((By.XPATH,'//button[@id="end_time_btn"]//img'))#点击结束时间
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/table/tbody/tr[4]/td[5]'))#选择结束时间22号
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/table/thead/tr[1]/td[3]/div'))
        ordst3 = self.select_option((By.ID, 'select9'), 2)#选择订单状态的下拉框
        time.sleep(2)
        ordst4 = self.select_option((By.ID, 'select11'), 3)#选择付款状态下拉框,选择已付款
        time.sleep(2)
        ordst5 = self.select_option((By.ID, 'select10'), 1)#选择收货状态下拉框
        time.sleep(2)
        self.click((By.ID,'query'))#点击搜索按钮
        time.sleep(3)





    def alterorder2(self):#将订单修改成未付款
        time.sleep(3)
        self.click((By.XPATH, '//*[@id="listDiv"]/table[1]/tbody/tr[3]/td[7]/a'))
        self.scroll_target((By.XPATH,"//input[@name='unpay']"))
        self.click((By.XPATH,"//input[@name='unpay']"))
        self.click((By.XPATH,'//textarea[@name="action_note"]'))
        self.send_keys((By.XPATH,'//textarea[@name="action_note"]'),'未付款')
        self.click((By.XPATH,"//input[@name='refund'and@value='1']"))
        self.click((By.XPATH,"//input[@class='button'and@name='submit']"))






















if __name__ == '__main__':
    driver=open_browser()
    so=SearchOrder(driver)
    so.input_url('http://139.129.26.163/admin')
    so.bk_username('admin')
    so.bk_password('admin123')
    so.login_bnt()
    so.click_order()
    so.search_order_num()
    so.search_order_hum()
    so.search_order_status()
    so.search_button()
    so.high_search()
    so.high_search_input()
    so.order_alter_pay()
    time.sleep(2)
    so.clickorderlist()
    time.sleep(2)
    so.high_search1()
    time.sleep(2)
    so.high_search_input1()
    time.sleep(2)
    so.alterorder2()











